<?php

namespace App\Http\Requests;

use App\Models\Cashflow;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class StatisticRequest extends FormRequest
{
    /**
     * 用户是否有权限请求
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * 精确到控制器方法的验证
     *
     * @return array
     */
    public function rules()
    {
        $actionMethod = request()->route()->getActionMethod();
        $ruleMethod = $actionMethod . 'Rule';
        if (method_exists($this, $ruleMethod)) {
            return $this->{$ruleMethod}();
        }

        return [];
    }

    private function storeRule()
    {
        $rule = [];

        return $rule;
    }

    private function updateRule()
    {
        $rule = [];

        return $rule;
    }

    public function statisticByTypeRule()
    {
        $rule = [
            "query_type" => ['required', 'string', Rule::in([
                'year', 'month'
            ])],
            'type' => [
                'required',
                'integer',
                Rule::in([Cashflow::TYPE_IN, Cashflow::TYPE_OUT])
            ],
            'year' => 'required|integer|min:2010',
            'month' => [
                'integer',
                'between:1,12'
            ]
        ];
        if (request('type') == 'month') {
            $rule['month'][] = 'required';
        } else {
            $rule['month'][] = 'nullable';
        }
        return $rule;
    }

    public function attributes()
    {
        return [];
    }

    // 常用验证方法 https://learnku.com/docs/laravel/5.8/validation/3899#rule-accepted
}
